var app = getApp();
var Util= require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    itemNew:[],
    activityList:[],
    menus: [],
    bastBanner: [],
    bastInfo: '',
    bastList: [],
    fastInfo: '',
    fastList: [],
    firstInfo: '',
    firstList: [],
    salesInfo: '',
    likeInfo: [],
    lovelyBanner: [],
    benefit:[],
    indicatorDots: false,
    circular: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    parameter: {
      'navbar': '0',
      'return': '0',
      'title':'首页'
    },
    window: false,
    lineList: [],
    currentPosition:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.spid) app.globalData.spid = options.spid;
    if (options.scene) app.globalData.code = decodeURIComponent(options.scene);
  //  wx.navigateTo({
     
  //                     url: "/pages/ticket/car_ticket?ticket_type=0",
  //                   })
  },
  catchTouchMove: function (res) {
    return false
  },
  onColse:function(){
    this.setData({ window: false});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
      //获取地理位置
      // wx.getLocation({
      //   type: 'gcj02 ',
      //   success (res) {
      //   console.log(res)
      //   that.getCurrentPosition(res.latitude,res.longitude);   
      //   },
      //   fail (res) {
      //     console.log(res)
      //   }
      //  })

      wx.getLocation({
        type: 'wgs84',
        success: function(res) {
          that.getCurrentPosition(res.latitude,res.longitude);   
        },
          fail (res) {
          console.log(res)
        }
      })
  },

  // 搜索线路
  searchLine: function searchLine(){

    var it = this;
    var upLat = this.data.start_address?this.data.start_address.lat: this.data.currentPosition.lat;
    var upLng = this.data.start_address?this.data.start_address.lng:this.data.currentPosition.lng;
    var downLat = this.data.end_address?this.data.end_address.lat:'';
    var downLng = this.data.end_address?this.data.end_address.lng:'';
    // if(!upLat&&upLng){
    //   upLat = this.data.currentPosition.lat;
    //   upLng = this.data.currentPosition.lng;
    // }
    if(!(upLat&&upLng)){
      wx.showToast({
          title: "请选择目的地",
          icon: "none"
      });
      return;
    }else if(!(downLat&&downLng)){
      wx.showToast({
          title: "请选择目的地",
          icon: "none"
      });
      return;
    } else{ 
      wx.navigateTo({
        url: '/pages/search_line/search_line?upLat='+upLat+'&upLng='+upLng+'&downLat='+downLat+'&downLng='+downLng,
      })

    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
 
  onShow: function () {
    // this.getIndexConfig();
    this.get_tj_line();
    var that = this;
    // if(app.globalData.isLog && app.globalData.token) this.get_issue_coupon_list();

    wx.getStorage({
      key:'start_position',
      success:function(res){
        if (res.data){
          that.setData({
            start_address: {
              name: res.data.name,
              lat: res.data.lat,
              lng: res.data.lng
            }
          })
        }else{
        }
      },
      fail(res){
      },
    });
    wx.getStorage({
      key:'end_position',
      success:function(res){
        if (res.data){
          that.setData({
            end_address: {
              name: res.data.name,
              lat: res.data.lat,
              lng: res.data.lng
            }
          })
        }else{
        }
      },
      fail(res){
      },
    });

      
      // wx.navigateTo({
      //   url: '/pages/search_address/search_address',
      // })
  },
          /**
   * 获取用户当前位置
   */
  
  getCurrentPosition: function getCurrentPosition(latitude,longitude) {
      
    var that = this
   

       if(latitude&&longitude){
       app.baseGet(app.U({ c: 'public_api', a:'get_user_location', q: { latitude:latitude,longitude:longitude}}),function(res){
        if(res.code==200){
            that.setData({
            currentPosition: {
                address:res.data.result.address,
                title: res.data.result.formatted_addresses.recommend,
                location: res.data.result.location,
                lat: res.data.result.location.lat,
                lng: res.data.result.location.lng,
            }
        });
        } else{
            that.setData({
                currentPosition: []
            });
        }
    });
}else{
    wx.showToast({
        title: '获取位置失败',
        icon: 'none',
        duration: 1000,
        mask: true,
      });
}
},

bannerEvent: function bannerEvent() {
  wx.navigateTo({ url: '/pages/goods_list/goods_list?title=' + '景区直通车'})
},
  // get_issue_coupon_list:function(){
  //   var that = this;
  //   app.baseGet(app.U({ c: 'coupons_api', a:'get_issue_coupon_list',q:{limit:3}}),function(res){
  //     that.setData({couponList:res.data});
  //     if (!res.data.length) that.setData({ window: false });
  //   });
  // },
  // getIndexConfig:function(){
  //   var that = this;
  //   var url = app.U({ c: 'public_api', a: 'index' }, app.globalData.url);
  //   app.baseGet(url,function(res){      
  //     that.setData({ 
  //       imgUrls: res.data.banner, 
  //       menus: res.data.menus, 
  //       itemNew: res.data.roll,
  //       activityList: res.data.activity,
  //       bastBanner: res.data.info.bastBanner,
  //       bastInfo: res.data.info.bastInfo,
  //       bastList: res.data.info.bastList,
  //       fastInfo: res.data.info.fastInfo,
  //       fastList: res.data.info.fastList,
  //       firstInfo: res.data.info.firstInfo,
  //       firstList: res.data.info.firstList,
  //       salesInfo: res.data.info.salesInfo,
  //       likeInfo: res.data.likeInfo,
  //       lovelyBanner:res.data.info,
  //       benefit: res.data.benefit,
  //       logoUrl: res.data.logoUrl,
  //       couponList: res.data.couponList,
  //     });
  //     wx.getSetting({
  //       success(res) {
  //         if (!res.authSetting['scope.userInfo']) {
  //           that.setData({ window: that.data.couponList.length ? true : false});
  //         }else{
  //           that.setData({ window: false });
  //         }
  //       }
  //     });
  //   });
  // },
  get_tj_line:function(){
    var that = this;
    var url = app.U({ c: 'public_api', a: 'get_tj_line' ,q:{first:0,limit:20}}, app.globalData.url);
    app.baseGet(url,function(res){  
      that.setData({
        lineList: res.data
      })
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ window:false});
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.getIndexConfig();
    // if (app.globalData.isLog && app.globalData.token) this.get_issue_coupon_list();
    // wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})